# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import logging

from configobj import ConfigObj, Section

_log = logging.getLogger(__name__)

CONFIG_SPEC_PATH = 'mediagoblin_local.ini'

COMMENT_LIST = ['',
                '## Uncomment this to turn on video or enable other media types',
                '## You may have to install dependencies, and will have to run ./bin/gmg dbupdate',
                '## See http://docs.mediagoblin.org/siteadmin/media-types.html for details.',
                '# media_types = mediagoblin.media_types.image, mediagoblin.media_types.video']


def config_section_merge(parent_section, old_section):
    for k, v in old_section.iteritems():
        if isinstance(v, Section):
            v = dict(v)
        parent_section[k] = v


def converter():
    if not os.path.exists(CONFIG_SPEC_PATH):
        _log.info('No {0} file, so skipping config conversion'.format(
            CONFIG_SPEC_PATH))
        return

    config = ConfigObj(CONFIG_SPEC_PATH)

    # add basic_auth plugin
    config['plugins']['mediagoblin.plugins.basic_auth'] = {}

    # add media_types as plugins
    media_types = config['mediagoblin'].pop('media_types', None)
    if media_types:
        for media_type in media_types:
            config['plugins'][media_type] = {}
    else:
        config['plugins']['mediagoblin.media_types.image'] = {}
        # Not sure why the media_type comments are being parsed to
        # 'storage:queuestore'
        comments = config.comments['storage:queuestore']
        for c in COMMENT_LIST:
            try:
                comments.remove(c)
            except ValueError:
                pass

    # move media_type config setting to plugin section
    image_config = config.pop('media_type:mediagoblin.media_types.image', None)
    if image_config:
        config_section_merge(
            config['plugins']['mediagoblin.media_types.image'],
            image_config)

    pdf_config = config.pop('media_type:mediagoblin.media_types.pdf', None)
    if pdf_config:
        config_section_merge(
            config['plugins']['mediagoblin.media_types.pdf'],
            pdf_config)

    ascii_config = config.pop('media_type:mediagoblin.media_types.ascii', None)
    if ascii_config:
        config_section_merge(
            config['plugins']['mediagoblin.media_types.ascii'],
            ascii_config)

    audio_config = config.pop('media_type:mediagoblin.media_types.audio', None)
    if audio_config:
        config_section_merge(
            config['plugins']['mediagoblin.media_types.audio'],
            audio_config)

    video_config = config.pop('media_type:mediagoblin.media_types.video', None)
    if video_config:
        config_section_merge(
            config['plugins']['mediagoblin.media_types.video'],
            video_config)

    config.write()

    print 'Your {0} file has been converted'

if __name__ == '__main__':
    converter()
